﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Design;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Reflection;

namespace SPVisualDev.VSAddin.Dialogs.PropertySheets
{
    internal class EventReceiverCollectionEditor : CollectionEditor
    {
        private Type[] m_types;

        public EventReceiverCollectionEditor(Type type)
            : base(type)
        {
            m_types = new Type[] { typeof(EventReceiverInfo) };
        }

        protected override Type CreateCollectionItemType()
        {
            return typeof(EventReceiverInfo);
        }

        protected override Type[] CreateNewItemTypes()
        {
            return m_types;
        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            return base.EditValue(context, provider, value);
        }

        protected override System.Collections.IList GetObjectsFromInstance(object instance)
        {
            return base.GetObjectsFromInstance(instance);
        }

        protected override object CreateInstance(Type itemType)
        {
            EventReceiverInfo info = new EventReceiverInfo();
            return info;
        }

        protected override bool CanRemoveInstance(object value)
        {
            EventReceiverInfo evtInfo = value as EventReceiverInfo;

            return !evtInfo.IsGeneratedFromCodeFile;
        }

        protected override CollectionForm CreateCollectionForm()
        {
            CollectionForm form = base.CreateCollectionForm();

            form.Text = "Event registrations editor";
            form.Width = 720;

            PropertyGrid grid = form.Controls.Find("propertyBrowser", true)[0] as PropertyGrid;
            grid.SelectedObjectsChanged += new EventHandler(grid_SelectedObjectsChanged);

            (form.Controls.Find("listbox", true)[0] as ListBox).Sorted = true;

            (form.Controls.Find("upButton", true)[0] as Button).Visible = false;
            (form.Controls.Find("downButton", true)[0] as Button).Visible = false;

            return form;
        }

        void grid_SelectedObjectsChanged(object sender, EventArgs e)
        {
            PropertyGrid grid = sender as PropertyGrid;
            if (grid.SelectedObject == null)
                return;

            EventReceiverInfo info = (EventReceiverInfo)grid.SelectedObject;

            SetReadOnlyFlag(info, "AssemblyName", info.IsGeneratedFromCodeFile);
            SetReadOnlyFlag(info, "AssemblyClass", info.IsGeneratedFromCodeFile);
            SetReadOnlyFlag(info, "EventType", info.IsGeneratedFromCodeFile);
        }

        private void SetReadOnlyFlag(object instance, string PropertyName, bool IsReadOnly)
        {
            PropertyDescriptor descriptor = TypeDescriptor.GetProperties(instance.GetType())[PropertyName];
            ReadOnlyAttribute attrib = (ReadOnlyAttribute)descriptor.Attributes[typeof(ReadOnlyAttribute)];
            FieldInfo isReadOnly = attrib.GetType().GetField("isReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
            isReadOnly.SetValue(attrib, IsReadOnly);
        }

    }

}
